# -*- coding: utf-8 -*-
# @Time : 2021/9/9 16:29
# @Author : 阿怪
# @Email : scg@gmail.com
# @File : CRM-ST-TJFX-021.py
# @Project : crm自动化测试项目实战

'''验证客户总计的搜索功能—正确输入搜索信息'''

from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.support.select import Select
from time import sleep
from selenium.webdriver.common.alert import Alert  # 把弹窗封装成类Alert

driver = webdriver.Chrome()
cookie = {'name': 'PHPSESSID', 'value': '0onrpth7nsd6mbi1hfgi99o2b1'}  # 获取到cookid
driver.get('http://192.168.1.140/')  # 访问服务器
driver.add_cookie(cookie)
driver.get('http://192.168.1.140/crm3/index.php?m=customer&a=index')
#进入客户首页
try:
    check_all_locator = (By.ID,'check_all')  #全选按钮定位器
    operation_locator = (By.CSS_SELECTOR,'body > div.container > div.row > div:nth-child(1) > ul > div > a')  #批量操作定位器
    add_client_locator =(By.CSS_SELECTOR,'body > div.container > div.row > div:nth-child(1) > div > a')  #添加客户按钮定位器

    #先去添加一条客户数据
    driver.find_element(*add_client_locator).click()
    driver.find_element(By.ID,'name').send_keys('张山123')    #这里这个客户名不能重复，每一次都要改变。。
    driver.find_element(By.NAME,'submit').click()

    # 进入客户统计页面
    driver.get('http://192.168.1.140/crm3/index.php?m=customer&a=analytics')

    select_department_locator = driver.find_element(By.ID,'department')   #选择部门定位器
    select_role_locator = driver.find_element(By.ID,'role')    #选择人员定位器
    start_time_locator = (By.ID,'start_time')     #开始日期定位器
    start_time_element = driver.find_element(*start_time_locator)
    end_time_locator = (By.ID,'end_time')       #结束日期
    end_time_element = driver.find_element(*end_time_locator)
    search_locator = (By.CSS_SELECTOR,'#searchForm > ul > li:nth-child(4) > button')    #搜索按钮定位器
    today = time.strftime('%Y-%m-%d')  # 获取当前日期


    # 1.第一个下拉框选择办公室
    select = Select(select_department_locator)
    select.select_by_value('1')
    # 2.第二个下拉框选择admin
    select1 = Select(select_role_locator)
    sleep(3)
    select1.select_by_visible_text('CEO-admin')
    #3.第三个下拉框选择今天
    start_time_element.send_keys(today)
    #4.第四个下拉框选择今天
    end_time_element.send_keys(today)
    #5.点击【搜索】
    driver.find_element(*search_locator).click()

    #每条用例都要清理数据。。。。回到客户界面把添加的线索全部删掉
    driver.find_element(By.CSS_SELECTOR,'body > div.container > div.page-header > ul > li:nth-child(1) > a').click()
    driver.find_element(By.ID,'check_all').click()   #点击全选按钮
    driver.find_element(*operation_locator).click()  #点击批量操作
    sleep(3)
    driver.find_element_by_xpath('/html/body/div[5]/div[2]/div[1]/ul/div/ul/li[1]/a').click()  #点击批量删除
    alert = Alert(driver)
    alert.accept()
    sleep(3)
    driver.quit()

except Exception as e:
    raise e